﻿Imports System.Data
Imports System.Data.SqlClient

Namespace DB

    ''' <summary>
    ''' TypeName:    UserRightType
    ''' Author:      Waahid
    ''' DBTableName: SEC_UsersRights
    ''' </summary>
    ''' <remarks></remarks>

    Public Class UserRightType
        Inherits DBTableObjectBase


#Region "Declarations"
        'Protected Friend iUserRightsId as Guid
        Protected Friend iUserId As Guid
        Protected Friend iRightsId As Guid

#End Region

#Region "Constructors"
        Public Sub New()
            MyBase.New(TableNamesEnum.SEC_UsersRights)
        End Sub
        Public Sub New(ByVal UserRightsId As Guid)
            MyBase.New(TableNamesEnum.SEC_UsersRights, UserRightsId)
        End Sub
        Public Sub New(ByVal UserRightsId As Guid, _
                        ByVal UserId As Guid, _
                        ByVal RightsId As Guid, _
                        ByVal LMUserId As Guid, _
                        ByVal LMDate As DateTime)
            MyBase.New(TableNamesEnum.SEC_UsersRights)
            Me.iId = UserRightsId
            Me.iUserId = UserId
            Me.iRightsId = RightsId
            Me.iLMUserId = LMUserId
            Me.iLMDate = LMDate

        End Sub
#End Region

#Region "Exposed Properties"
        Public Property UserRightsId() As Guid
            Get
                Return Me.iId
            End Get
            Set(ByVal value As Guid)
                Me.iId = value
            End Set
        End Property
        Public Property UserId() As Guid
            Get
                Return Me.iUserId
            End Get
            Set(ByVal value As Guid)
                Me.iUserId = value
            End Set
        End Property
        Public Property RightsId() As Guid
            Get
                Return Me.iRightsId
            End Get
            Set(ByVal value As Guid)
                Me.iRightsId = value
            End Set
        End Property
#End Region

#Region "Implementations"
        Protected Friend Overloads Function Load(ByVal Row As DataRow) As Boolean
            If IsDBNull(Row(0)) Then Me.iId = Guid.Empty Else Me.iId = Row(0)
            If IsDBNull(Row(1)) Then Me.iUserId = Nothing Else Me.iUserId = Row(1)
            If IsDBNull(Row(2)) Then Me.iRightsId = Nothing Else Me.iRightsId = Row(2)
            If IsDBNull(Row(3)) Then Me.iLMUserId = Nothing Else Me.iLMUserId = Row(3)
            If IsDBNull(Row(4)) Then Me.iLMDate = Nothing Else Me.iLMDate = Row(4)

            Return True
        End Function
        Protected Friend Overrides Function Load() As Boolean
            'sp_[TableName]Get
            Dim resBool As Boolean = True
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Get '" & Me.iId.ToString("D") & "'")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction

            Dim dt As DataTable = New DataTable
            dt = Utility.GetList(cmd)

            If dt.Rows.Count > 0 Then
                Me.Load(dt.Rows(0))
            Else
                resBool = False
            End If

            Return resBool
        End Function
        Protected Friend Overrides Function Insert() As Boolean
            'sp_[TableName]Insert
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter

            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Insert")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If IsNothing(Me.iId) Then
                        .Add(New SqlParameter("@UserRightsId", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@UserRightsId", Me.iId))
                    End If
                    If IsNothing(Me.iUserId) Then
                        .Add(New SqlParameter("@UserId", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@UserId", Me.iUserId))
                    End If
                    If IsNothing(Me.iRightsId) Then
                        .Add(New SqlParameter("@RightsId", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@RightsId", Me.iRightsId))
                    End If
                    If IsNothing(Me.iLMUserId) Then
                        .Add(New SqlParameter("@LMUserId", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@LMUserId", Me.iLMUserId))
                    End If
                    If IsNothing(Me.iLMDate) Then
                        .Add(New SqlParameter("@LMDate", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@LMDate", Me.iLMDate))
                    End If

                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to insert, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function Update() As Boolean
            'sp_[TableName]Update
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Update")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If IsNothing(Me.iId) Then
                        .Add(New SqlParameter("@UserRightsId", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@UserRightsId", Me.iId))
                    End If
                    If IsNothing(Me.iUserId) Then
                        .Add(New SqlParameter("@UserId", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@UserId", Me.iUserId))
                    End If
                    If IsNothing(Me.iRightsId) Then
                        .Add(New SqlParameter("@RightsId", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@RightsId", Me.iRightsId))
                    End If
                    If IsNothing(Me.iLMUserId) Then
                        .Add(New SqlParameter("@LMUserId", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@LMUserId", Me.iLMUserId))
                    End If
                    If IsNothing(Me.iLMDate) Then
                        .Add(New SqlParameter("@LMDate", System.DBNull.Value))
                    Else
                        .Add(New SqlParameter("@LMDate", Me.iLMDate))
                    End If

                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to update, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function ValidateOnDelete() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnInsert() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnUpdate() As Boolean
            Return True
        End Function
#End Region

#Region "Overrides"
        Public Overrides Function ToString() As String
            Return Me.iId.ToString
        End Function
        Public Overrides Function ToDetailsListBoxItem() As WindowsExtendedControls.DetailListBoxItem
            Return Nothing
        End Function
#End Region

    End Class

Public Class UserRightTypeCollection
    Inherits DBTableObjectCollectionBase

    Public DataFilters As New DataFilterTypeCollection(Of UserRightTypeFields)(Me.iTablename.ToString)
    Public DataSorters As New DataSorterTypeCollection(Of UserRightTypeFields)(Me.iTableName.ToString)

    Public Sub New()
        MyBase.New(TableNamesEnum.SEC_UsersRights)
    End Sub

    Default Public Property Item(ByVal Index As Integer) As UserRightType
        Get
            Return CType(Me.List.Item(Index), UserRightType)
        End Get
        Set(ByVal value As UserRightType)
            Me.List.Item(Index) = value
        End Set
    End Property

    Protected Friend Overrides Function Load() As Boolean
        Return Me.Load("SELECT * FROM " & Me.iTableName.ToString & Me.DataFilters.GetSQL & Me.DataSorters.GetSQL)
    End Function

    Protected Friend Overloads Function Load(ByVal CustomFilter As String) As Boolean
        'Get data from DB
        Dim cmd As New SqlCommand
        cmd.Connection = Me.iConnection
        If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
        Dim dt As DataTable
        Try
            cmd.CommandText = CustomFilter
            dt = Utility.GetList(cmd)
        Catch ex As Exception
            Throw ex
        End Try
        'Clear exising list
        Dim row As DataRow
        List.Clear()

        'Build new list
        Dim typeObject As UserRightType

        For Each row In dt.Rows
            typeObject = New UserRightType()
            typeObject.iConnection = Me.iConnection
            typeObject.iTransaction = Me.iTransaction
            If typeObject.Load(row) = True Then Me.List.Add(typeObject)
        Next
        Return True
    End Function

    Public Overloads Sub Sort(ByVal propertyToSort As UserRightTypeFields, ByVal sortOrder As SortOrder)
        ipropertyToSort = propertyToSort.ToString
        isortOrder = sortOrder
        Me.InnerList.Sort(Me)
    End Sub

End Class

#Region "Enumerations"       'Enumerations
    Public Enum UserRightTypeFields
        UserRightsId
        UserId
        RightsId
        LMUserId
        LMDate

    End Enum
#End Region

End Namespace